package cn.iocoder.yudao.module.cont.service.deal;

import cn.iocoder.yudao.module.cont.controller.admin.deal.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.*;
import cn.iocoder.yudao.module.cont.dal.mysql.base.PayInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.deal.DealMapper;
import cn.iocoder.yudao.module.cont.enums.base.ContHtlxEnum;
import cn.iocoder.yudao.module.cont.enums.base.ContHtztEnum;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 合同基础信息表  Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DealServiceImpl implements DealService {

    @Resource
    private DealMapper dealMapper;
    @Resource
    private NotifyMessageSendApi notifySendApi;
    @Resource
    private PayInfoMapper payInfoMapper;

    @Override
    public List<BaseInfoDO> getDueCont() {
        QueryWrapper<BaseInfoDO> queryWrapper = new QueryWrapper<>();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        // 获取七天后的日期
        LocalDate dueDay = today.plusDays(7);
        queryWrapper.between("jssj",today,dueDay);
        return dealMapper.selectList(queryWrapper);
    }

    @Override
    public Long sendHtCount() {
        // 1. 准备参数
        Long userId = 1L; // 示例中写死，你可以改成你业务中的 userId 噢
        String templateCode = "002"; // 站内信模版，记得在【站内信管理】中配置噢
        Map<String, Object> templateParams = new HashMap<>();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        // 获取七天后的日期
        LocalDate dueDay = today.plusDays(7);
        templateParams.put("weekDay", dueDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //查询7天的条数
        QueryWrapper<BaseInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("jssj",today,dueDay);
        Long count = dealMapper.selectCount(queryWrapper);
        templateParams.put("number", count);
        // 2. 发送站内信
        if(count != 0){
            notifySendApi.sendSingleMessageToAdmin(new NotifySendSingleToUserReqDTO()
                    .setUserId(userId).setTemplateCode(templateCode).setTemplateParams(templateParams));
        }
        return count;
    }

    @Override
    public DealIndexRespVo getIndexInfo() {
        // 获取当天日期
        LocalDate today = LocalDate.now();
        // 获取七天后的日期
        LocalDate dueDay = today.plusDays(7);
        DealIndexRespVo dealIndexRespVo = new DealIndexRespVo();
        //设置当天日期
        dealIndexRespVo.setCurrentDate(today.atStartOfDay());
        //查询7天的条数
        QueryWrapper<BaseInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("jssj",today,dueDay);
        dealIndexRespVo.setDueContNum(dealMapper.selectCount(queryWrapper));
        //查询合同总数
        dealIndexRespVo.setContCount(dealMapper.selectCount());
        //实际收付款综合
        dealIndexRespVo.setPayTotal(payInfoMapper.getTotalMoney());
        return dealIndexRespVo;
    }

    @Override
    public DealIndexContRespVo getIndexCont() {
        DealIndexContRespVo dealIndexContRespVo = new DealIndexContRespVo();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        dealIndexContRespVo.setCurrentDate(today.atStartOfDay());
        System.out.println("当天日期: " + today);

        // 获取当天所在周的周日
        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        System.out.println("当天所在周的周日: " + sunday);
        QueryWrapper<BaseInfoDO> weekQueryWrapper = new QueryWrapper<>();
        weekQueryWrapper.between("jssj",today,sunday);
        dealIndexContRespVo.setDueToWeekCont(dealMapper.selectCount(weekQueryWrapper));

        // 获取当天所在季度的最后一天
        LocalDate lastDayOfQuarter = today.with(TemporalAdjusters.lastDayOfMonth())
                .with(TemporalAdjusters.lastDayOfMonth())
                .with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("当天所在季度的最后一天: " + lastDayOfQuarter);
        QueryWrapper<BaseInfoDO> quarterQueryWrapper = new QueryWrapper<>();
        quarterQueryWrapper.between("jssj",today,lastDayOfQuarter);
        dealIndexContRespVo.setDueToQuarterCont(dealMapper.selectCount(quarterQueryWrapper));
        // 获取当天所在月份的最后一天
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("当天所在月份的最后一天: " + lastDayOfMonth);
        QueryWrapper<BaseInfoDO> monthQueryWrapper = new QueryWrapper<>();
        monthQueryWrapper.between("jssj",today,lastDayOfMonth);
        dealIndexContRespVo.setDueToMonthCont(dealMapper.selectCount(monthQueryWrapper));
        return dealIndexContRespVo;
    }

    @Override
    public DealIndexPayRespVo getIndexPay() {
        DealIndexPayRespVo dealIndexPayRespVo = new DealIndexPayRespVo();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        dealIndexPayRespVo.setCurrentDate(today.atStartOfDay());

        // 获取当天所在周的周日
        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        QueryWrapper<PayInfoDO> weekQueryWrapper = new QueryWrapper<>();
        weekQueryWrapper.between("ysfrq",today,sunday);
        dealIndexPayRespVo.setDueToWeekPay(payInfoMapper.selectCount(weekQueryWrapper));

        // 获取当天所在季度的最后一天
        LocalDate lastDayOfQuarter = today.with(TemporalAdjusters.lastDayOfMonth())
                .with(TemporalAdjusters.lastDayOfMonth())
                .with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("当天所在季度的最后一天: " + lastDayOfQuarter);
        QueryWrapper<PayInfoDO> quarterQueryWrapper = new QueryWrapper<>();
        quarterQueryWrapper.between("ysfrq",today,lastDayOfQuarter);
        dealIndexPayRespVo.setDueToQuarterPay(payInfoMapper.selectCount(quarterQueryWrapper));
        // 获取当天所在月份的最后一天
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("当天所在月份的最后一天: " + lastDayOfMonth);
        QueryWrapper<PayInfoDO> monthQueryWrapper = new QueryWrapper<>();
        monthQueryWrapper.between("ysfrq",today,lastDayOfMonth);
        dealIndexPayRespVo.setDueToMonthPay(payInfoMapper.selectCount(monthQueryWrapper));
        return dealIndexPayRespVo;
    }

    @Override
    public DealIndexContStatusRespVo getIndexContStatus() {
        DealIndexContStatusRespVo dealIndexContStatusRespVo = new DealIndexContStatusRespVo();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        dealIndexContStatusRespVo.setCurrentDate(today.atStartOfDay());
        //获取不同状态的合同
        dealIndexContStatusRespVo.setAbandonedCont(dealMapper.getContStatus(ContHtztEnum.FKFS_FQ.getStatus()));
        dealIndexContStatusRespVo.setExpiredCont(dealMapper.getContStatus(ContHtztEnum.HTZT_GQ.getStatus()));
        dealIndexContStatusRespVo.setCompleteCont(dealMapper.getContStatus(ContHtztEnum.HTZT_QSWC.getStatus()));
        dealIndexContStatusRespVo.setNewlySignedCont(dealMapper.getContStatus(ContHtztEnum.HTZT_XQZ.getStatus()));
        dealIndexContStatusRespVo.setRenewalCont(dealMapper.getContStatus(ContHtztEnum.HTZT_QQZ.getStatus()));
        dealIndexContStatusRespVo.setExecutingCont(dealMapper.getContStatus(ContHtztEnum.HTZT_ZZX.getStatus()));
        return dealIndexContStatusRespVo;
    }

    @Override
    public Long sendPayCount() {
        // 1. 准备参数
        Long userId = 1L; // 示例中写死，你可以改成你业务中的 userId 噢
        String templateCode = "003"; // 站内信模版，记得在【站内信管理】中配置噢
        Map<String, Object> templateParams = new HashMap<>();
        // 获取当天日期
        LocalDate today = LocalDate.now();
        // 获取七天后的日期
        LocalDate dueDay = today.plusDays(7);
        templateParams.put("weekDay", dueDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //查询7天的条数
        QueryWrapper<PayInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("ysfrq",today,dueDay);
        Long count = payInfoMapper.selectCount(queryWrapper);
        templateParams.put("number", count);
        // 2. 发送站内信
        if(count != 0){
            notifySendApi.sendSingleMessageToAdmin(new NotifySendSingleToUserReqDTO()
                    .setUserId(userId).setTemplateCode(templateCode).setTemplateParams(templateParams));
        }
        return count;
    }

    @Override
    public DealIndexContTypeRespVo getIndexContType() {
        // 获取当天日期
        LocalDate today = LocalDate.now();
        DealIndexContTypeRespVo dealIndexContTypeRespVo = new DealIndexContTypeRespVo();
        //设置当天日期
        dealIndexContTypeRespVo.setCurrentDate(today.atStartOfDay());
        //获取不同类型的合同
        dealIndexContTypeRespVo.setSalesCont(dealMapper.getContType(ContHtlxEnum.XSHT.getStatus()));
        dealIndexContTypeRespVo.setConstructionCont(dealMapper.getContType(ContHtlxEnum.JSHT.getStatus()));
        dealIndexContTypeRespVo.setPurchseCont(dealMapper.getContType(ContHtlxEnum.GMHT.getStatus()));
        return dealIndexContTypeRespVo;
    }


}